<?php
namespace Admin\Controller;

use Think\Controller;
use Think\Page;
class AdminController extends IndexController{
	public function index(){
		$model=D('Admin');
		$data=$model->search();
		//p($data);die;
		$this->assign(array(
			'data'=>$data['data'],
			'page'=>$data['page']
		));
		$this->display();
	}
	public function add(){
		if(IS_POST){
			$model=D('Admin');
			if($model->create()){
				//$model->password=md5($model->password);
				if($model->add()){
					$this->success('添加成功',U('index'));
					exit;
				}else{
					$this->error('添加失败');
				}
			}else{
				$this->error($model->getError());
			}
		}
		$this->display();
	}
	public function delete($id){
		$model=M('Admin');
		$res=$model->delete($id);
		
		if($res){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
		
	}
	public function edit($id){
		$model=D('Admin');
		if(IS_POST){			
			if($model->create()){
				//$model->password=md5($model->password);
				//save 成功返回mysql_affected_rows(),失败返回false
				if($model->save()!==FALSE){
					$this->success('修改成功',U('index'));
					exit;
				}else{
					$this->error('修改失败');
				}
			}else{
				$this->error($model->getError());
			}
		} 
		$id=I('id');
		$this->data=$model->find($id);
		$this->display();
	}
	public function deleteAll(){
		$delid=I('post.delid');
		if(!IS_POST){
			$this->error('非法请求');
		}else{
			if($delid){				
				$bool=array_search(1, $delid);
				if($bool!==FALSE){
					unset($delid[$bool]);
				}
				if($delid){
					$delid=implode(',', $delid);
					$model=M('Admin');
					if($model->delete($delid)){
						$this->success('删除成功');
					}else{
						$this->error('删除失败');
					}
				}
			}
		}
	}
}